package logicGame;

import logicGame.Cards.*;
import java.awt.Color;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;

public class Player implements Serializable {

    boolean winner, ia;
    int gems, iron, wood, food, handpiece, joker, coin, coinsBet, victoryPoints;
    String name;
    ArrayList<Card> hand;
    ArrayList<Army> troop;
    ArrayList<City> empire;
    HashMap<Integer, Integer> empireOwned;
    Color color;
    
    public Player(String name, int coins, Color c, boolean ia) {
        this.name = name;
        this.coin = coins;
        this.coinsBet = 0;
        this.color = c;
        this.ia = ia;
        troop = new ArrayList<>();
        hand = new ArrayList<>();
        empire = new ArrayList<>();
        empireOwned = new HashMap<>();
        winner = false;
        gems = iron = wood = food = handpiece = joker = victoryPoints = 0;

        //inicializar os 5 continentes
        for (int i = 0; i < 5; i++) {
            empireOwned.put(i, 0);
        }
    }

    public void inicializeResources() {
        for (int i = 0; i < 14; i++) {
            troop.add(new Army(this, color));
            if (i < 3) {
                empire.add(new City(this, color));
            }
        }
    }

    public ArrayList<Card> getHand() {
        return hand;
    }

    public boolean isIa() {
        return ia;
    }
    
    public int getCoin() {
        return coin;
    }

    public Card getLastCard() {
        if(hand.isEmpty())
            return null;
        else{
        return hand.get(hand.size() - 1);
        }
    }

    public void setCoin(int coin) {
        this.coin += coin;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCoinsBet() {
        return coinsBet;
    }

    public void setCoinsBet(int coinsBet) {
        this.coinsBet = coinsBet;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "\nJogador " + name + ", moedas = " + coin + ", Numero de Exercitos = " + troop.size() + ", Numero Cartas = " + hand.size() + "\n\nRecursos:\n" + "Joias: " + gems + "\tAlimentos: " + food + "\tMadeira: " + wood + "\tFerro: " + iron + "\tUtensilios: " + handpiece + "\tJoker: " + joker;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Player other = (Player) obj;

        return Objects.equals(this.name, other.name);
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 79 * hash + Objects.hashCode(this.name);
        return hash;
    }

    public void buyCardP(Card c) {
        hand.add(c);
    }

    public boolean checkCash(int n) {
        int cost;

        if (n == 1) {
            cost = 0;
        } else if (n == 2 || n == 3) {
            cost = 1;
        } else if (n == 4 || n == 5) {
            cost = 2;
        } else {
            cost = 3;
        }

        if (coin >= cost) {
            coin -= cost;
            return true;
        } else {
            return false;
        }
    }

    public int getNTroops() {
        return troop.size();
    }

    public Army getTroop() {
        return troop.get(0);
    }

    public void removeTroop() {
        this.troop.remove(0);
    }

    public void receiveTroop(Army a) {
        troop.add(a);
    }

    public int getNEmpires() {
        return empire.size();
    }

    public City getEmpire() {
        return empire.get(0);
    }

    public void removeEmpire() {
        this.empire.remove(0);
    }

    public void addEmpireOwned(int continent) {

        if (!empireOwned.containsKey(continent)) {
            empireOwned.put(continent, 1);

        } else {
            Integer n = empireOwned.get(continent);
            empireOwned.remove(continent);
            empireOwned.put(continent, n + 1);
        }
    }

    public int getVictoryPoints() {
        return victoryPoints;
    }

    public void setVictoryPoints(int victoryPoints) {
        this.victoryPoints += victoryPoints;
    }

    public void actualizeResources() {
        String resource = getLastCard().getResource();
        switch (resource) {
            case "Joias":
                gems += 1;
                break;
            case "Alimentos":
                food += 1;
                break;
            case "Madeira":
                wood += 1;
                break;
            case "Ferro":
                iron += 1;
                break;
            case "Utensilios":
                handpiece += 1;
                break;
            case "Joker":
                joker += 1;
                break;
        }
    }

    public int countResources() {

        if (gems == 1) {
            victoryPoints += 1;

        } else if (gems == 2) {
            victoryPoints += 3;

        } else if (gems == 3) {
            victoryPoints += 4;

        } else if (gems >= 4) {
            victoryPoints += 5;
        }

        if (iron == 2 || iron == 3) {
            victoryPoints += 1;

        } else if (iron == 4) {
            victoryPoints += 2;

        } else if (iron == 5) {
            victoryPoints += 3;

        } else if (iron >= 6) {
            victoryPoints += 5;
        }

        if (wood == 2 || wood == 3) {
            victoryPoints += 1;

        } else if (wood == 4) {
            victoryPoints += 2;

        } else if (wood == 5) {
            victoryPoints += 3;

        } else if (wood >= 6) {
            victoryPoints += 5;
        }

        if (food == 3 || food == 4) {
            victoryPoints += 1;

        } else if (food == 5 || food == 6) {
            victoryPoints += 2;

        } else if (food == 7) {
            victoryPoints += 3;

        } else if (food >= 8) {
            victoryPoints += 5;
        }

        if (handpiece == 2 || handpiece == 3) {
            victoryPoints += 1;

        } else if (handpiece == 4 || handpiece == 5) {
            victoryPoints += 2;

        } else if (handpiece == 6) {
            victoryPoints += 3;

        } else if (handpiece >= 6) {
            victoryPoints += 5;
        }

        return victoryPoints;
    }

    public int getTotalEmpiresOwned() {

        int total = 0;

        //ciclo dos 
        for (int i = 0; i < empireOwned.size(); i++) {
            total += empireOwned.get(i);
        }
        return total;
    }

    public int getNEmpiresOwned(int i) {
        return empireOwned.get(i);
    }

    public void setWinner(boolean winner) {
        this.winner = winner;
    }

    public int getJoker() {
        return joker;
    }

    public void convertJoker(int op) {
        switch (op) {
            case 1:
                gems += 1;
                joker -= 1;
                break;

            case 2:
                iron += 1;
                joker -= 1;
                break;

            case 3:
                wood += 1;
                joker -= 1;
                break;

            case 4:
                food += 1;
                joker -= 1;
                break;

            case 5:
                handpiece += 1;
                joker -= 1;
                break;
        }
    }

}
